package org.eiahe.hr.employeeRelation.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import lombok.RequiredArgsConstructor;
import org.ehe.common.core.BaseController;
import org.ehe.common.core.domain.R;
import org.ehe.common.core.validate.AddGroup;
import org.ehe.common.core.validate.EditGroup;
import org.ehe.common.log.annotation.Log;
import org.ehe.common.log.enums.BusinessType;
import org.ehe.common.mybatis.core.page.PageQuery;
import org.ehe.common.mybatis.core.page.TableDataInfo;
import org.eiahe.hr.employeeRelation.domain.bo.ContractRenewalApplicationBo;
import org.eiahe.hr.employeeRelation.domain.bo.ContractTerminationApplicationBo;
import org.eiahe.hr.employeeRelation.domain.vo.ContractTerminationApplicationVo;
import org.eiahe.hr.employeeRelation.service.IContractTerminationService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * 合同终止控制器
 *
 * @author zhangnn
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/employeeRelation/contractTermination")
public class ContractTerminationController extends BaseController {

    private final IContractTerminationService contractTerminationService;

    /**
     * 查询合同终止申请列表
     */
    @SaCheckPermission("employeeRelation:contractTermination:list")
    @GetMapping("/application/list")
    public TableDataInfo<ContractTerminationApplicationVo> listApplication(ContractTerminationApplicationBo bo,
            PageQuery pageQuery) {
        return contractTerminationService.selectContractTerminationApplicationList(bo, pageQuery);
    }

    /**
     * 获取合同终止申请详细信息
     */
    @SaCheckPermission("employeeRelation:contractTermination:query")
    @GetMapping(value = "/application/{applicationId}")
    public R<ContractTerminationApplicationVo> getApplicationInfo(@PathVariable("applicationId") Long applicationId) {
        return R.ok(contractTerminationService.selectContractTerminationApplicationById(applicationId));
    }

    /**
     * 新增合同终止申请
     */
    @SaCheckPermission("employeeRelation:contractTermination:add")
    @Log(title = "新增合同终止申请", linkType = "termination", businessType = BusinessType.INSERT, nameField = "employeeNo")
    @PostMapping("/application")
    public R<Void> addApplication(@Validated(AddGroup.class) @RequestBody ContractTerminationApplicationBo bo) {
        return toAjax(contractTerminationService.insertContractTerminationApplication(bo));
    }

    /**
     * 修改合同终止申请
     */
    @SaCheckPermission("employeeRelation:contractTermination:edit")
    @Log(title = "修改合同终止申请", linkType = "termination", businessType = BusinessType.UPDATE, nameField = "employeeNo")
    @PutMapping("/application")
    public R<Void> editApplication(@Validated(EditGroup.class) @RequestBody ContractTerminationApplicationBo bo) {
        return toAjax(contractTerminationService.updateContractTerminationApplication(bo));
    }

    /**
     * 删除合同终止申请
     */
    @SaCheckPermission("employeeRelation:contractTermination:remove")
    @Log(title = "合同终止申请", businessType = BusinessType.DELETE)
    @DeleteMapping("/application/{applicationIds}")
    public R<Void> removeApplication(@PathVariable Long[] applicationIds) {
        return toAjax(contractTerminationService.deleteContractTerminationApplicationByIds(applicationIds));
    }

    /**
     * 审批通过终止申请-通过
     */
    @SaCheckPermission("employeeRelation:contractTermination:approve")
    @Log(title = "审批通过续签申请", businessType = BusinessType.UPDATE,linkType = "renewal",nameField = "employeeNo")
    @PostMapping("/application/approve")
    public R<Void> approveApplication(@RequestBody ContractTerminationApplicationBo bo) {
        return toAjax(contractTerminationService.approveContractTermination(bo));
    }

    /**
     * 审批通过终止申请-拒绝
     */
    @SaCheckPermission("employeeRelation:contractTermination:approve")
    @Log(title = "审批通过续签申请", businessType = BusinessType.UPDATE,linkType = "renewal",nameField = "employeeNo")
    @PostMapping("/application/reject")
    public R<Void> rejectApplication(@RequestBody ContractTerminationApplicationBo bo) {
        return toAjax(contractTerminationService.rejectContractTermination(bo));
    }
}
